#include "variablesviewwidget.h"
#include "mainwindow.h"
#include "datamanagerwidget.h"

#include <QDesktopWidget>
#include <QCloseEvent>
#include <QItemSelectionModel>
#include <QMenu>
#include <QTreeWidgetItem>
#include <QInputDialog>

//Need add variable delete function.

VariablesViewWidget::VariablesViewWidget(QWidget *parent) :
    QDockWidget(parent)
{
    treeView = new QTreeView(this);
    setWidget(treeView);
    setAllowedAreas(Qt::LeftDockWidgetArea |
                    Qt::RightDockWidgetArea);
    setMinimumHeight(parent->height()/3);
    setFeatures(QDockWidget::DockWidgetFloatable |
                QDockWidget::DockWidgetMovable);
//    setWindowFlags(Qt::MSWindowsFixedSizeDialogHint);
    MainWindow* parentWin = (MainWindow*)(this->parent());
    treeViewModel = new TreeViewModel(&(parentWin->dataSet), this);
    treeView->setModel(treeViewModel);
    setObjectName(tr("Variable"));
    setWindowTitle(tr("Variable"));
//    setStyleSheet("border: 5px solid transparent; padding: 0px;");
    popMenu = new QMenu(this);
    popMenu->addAction(tr("View"));
    popMenu->addAction(tr("Rename"));
    popMenu->addAction(tr("Duplicate"));
    popMenu->addAction(tr("Delete"));
    //selection changes shall trigger a slot
    //set context menu
    treeView->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(treeView, SIGNAL(customContextMenuRequested(QPoint)),
            this, SLOT(popMenuEditItem(QPoint)) );
}

VariablesViewWidget::~VariablesViewWidget()
{
    //all new object's parent is this, will auto-delete
}

void VariablesViewWidget::viewRefresh(QList<GSData*>* pDataSet)
{
    delete treeViewModel;
    treeViewModel = new TreeViewModel(pDataSet, this);
    treeView->setModel(treeViewModel);
    treeViewModel->refreshDataView(pDataSet);
}

void VariablesViewWidget::popMenuEditItem(const QPoint &pos)
{    
    if(treeView->indexAt(pos).isValid())
    {
        QAction *selected = popMenu->exec(
                    treeView->viewport()->mapToGlobal(pos));//(QCursor::pos());
        if(selected)
        {
            VariablesViewWidget::EditMode mode;
            if( selected->text() == tr("View"))
                mode = VariablesViewWidget::VIEW;
            else if(selected->text() == tr("Delete"))
                mode = VariablesViewWidget::DEL;
            else if(selected->text() == tr("Duplicate"))
                mode = VariablesViewWidget::COPY;
            else if(selected->text() == tr("Rename"))
                mode = VariablesViewWidget::RENAME;
            else
                return;

            const QModelIndex index =
                    treeView->selectionModel()->currentIndex();
            editVariable(index.row(), mode);
        }
    }
}

void VariablesViewWidget::editVariable(int index, EditMode mode)
{
    MainWindow* parentWin = (MainWindow*)(this->parent());
    GSData* pdata = parentWin->dataSet.at(index);

    switch (mode) {
    case VariablesViewWidget::VIEW:
        emit newDataView(pdata); //View Data
        break;
    case VariablesViewWidget::DEL:
        emit deleteVariable(index); //Delete Variable
        break;
    case VariablesViewWidget::COPY:
        emit duplicateVariable(pdata);//Copy Variable
        break;
    case VariablesViewWidget::RENAME:
        emit editVariableName(pdata);//Rename Variable
        break;
    default:
        break;
    }
}

